package ex.system.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import modules.system.entity.User;
import modules.system.entity.UserRoleRel;
import modules.system.service.UserService;

public class UserServiceEx extends UserService {
	public User getUserByLoginName(String loginName){
		List users = getUserDao().findBy("loginName",loginName);
		if(users.size()>0) return (User)users.get(0);
		return null;
	}
	
	public List getUsersByRoleName(String roleName) {
		return getUserDao()
				.find(
						"select u from User u left join u.userRoleRels ur left join ur.role r where u.status = '1' and ur.status = '1' and r.status = '1' and r.roleName = ?",
						new Object[] { roleName });
	}

	public String[] getRelativeRoleIds(Serializable userId) {
		List urs = getUserDao()
		.find(
				"select ur from UserRoleRel ur where ur.status = '1' and ur.user.status = '1' and ur.role.status = '1' and ur.user.id = ?",
				new Object[] { userId });

		String[] result = new String[urs.size()];
		int i = 0;
		for (Iterator iter = urs.iterator(); iter.hasNext();) {
			UserRoleRel userRoleRel = (UserRoleRel) iter.next();
			result[i] = userRoleRel.getRole().getId().toString();
			i++;
		}
		return result;
	}

	public void update(Object entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	public void saveOrUpdate(Object entity) {
		// TODO Auto-generated method stub
		super.saveOrUpdate(entity);
	}

	public void erase(Object entity) {
		// TODO Auto-generated method stub
		super.erase(entity);
	}

	public void delete(Object entity) {
		// TODO Auto-generated method stub
		super.delete(entity);
	}
	
	
}
